package com.whu.waterworks.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import com.sun.tools.internal.xjc.model.CNonElement;
import com.whu.waterworks.dto.GisTextureStationDto;
import com.whu.waterworks.dto.GisTextureWaterplantDto;
import com.whu.waterworks.model.WaterPlant;

public class ImageUtil {
	private final static Color IMAGE_TITLE_COLOR = new Color(255, 0, 0);
	private final static Color TEXT_COLOR = new Color(0x0000EE);
	private final static Color CONTENT_COLOR = new Color(0xDC143C);
	private final static Color UNIT_COLOR = new Color(0x000000);
	private static long times=0;

	public final static InputStream drawStation(GisTextureStationDto dto ,String path) {
		File img = new File(path);
		Image src=null;
		try {
			src = ImageIO.read(img);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int width = src.getWidth(null);
		int height = src.getHeight(null);
		int xClientOffset = 60;
		int yClientOffset = 60;
		int yOffset =(height-2*yClientOffset)/5;
		int drawY = yClientOffset +yOffset/2;
//		int xOffset = xClientOffset+(width-2*xClientOffset) / 5;
		int xOffset = xClientOffset;
		int fontSize = yOffset / 3 * 2;
		int contentfontSize = yOffset / 2;
		Color titleColor = IMAGE_TITLE_COLOR;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.drawImage(src, 0, 0, width, height, null);
		g.setComposite(AlphaComposite
				.getInstance(AlphaComposite.SRC_OVER, 1.0f));
		//draw 监测点名字
		String name = dto.getName()+""+times++;
		System.out.println("刷新---->"+name);
		Font titleFont = new Font("黑体", Font.PLAIN, fontSize);
		g.setFont(titleFont);
		g.setColor(titleColor);
		g.drawString(name, (width - (getLength(name) * fontSize)) / 2, drawY);
		
		//draw 监测点属性
		Font contentFont = new Font("宋体", Font.PLAIN, contentfontSize);
		g.setFont(contentFont);
		String content = null;
		
		xOffset = xClientOffset;
		content = dto.getZhuodu();
		if(content!=null)
		{
			content+=" ";
			//名字
			g.setColor(TEXT_COLOR);
			drawY += yOffset;
			g.drawString("浊度：", xOffset, drawY);
			xOffset+= getLength("浊度：")*contentfontSize;
			
			//变量值
			g.setColor(CONTENT_COLOR);
			g.drawString(content, xOffset, drawY);
			System.out.println(getLength(content));
			xOffset+= getLength(content)*contentfontSize;
			
			//单位
			g.setColor(UNIT_COLOR);
			g.drawString("NTU", xOffset, drawY);
			
		}
		
		xOffset = xClientOffset;
		content = dto.getYulv();
		if(content!=null)
		{
			content+=" ";
			//名字
			g.setColor(TEXT_COLOR);
			drawY += yOffset;
			g.drawString("余氯：", xOffset, drawY);
			xOffset+= getLength("余氯：")*contentfontSize;
			
			//变量值
			g.setColor(CONTENT_COLOR);
			g.drawString(content, xOffset, drawY);
			System.out.println(getLength(content));
			xOffset+= getLength(content)*contentfontSize;
			
			//单位
			g.setColor(UNIT_COLOR);
			g.drawString("mg/L", xOffset, drawY);
			
		}
		
		
		xOffset = xClientOffset;
		content=dto.getYali();
		if(content!=null)
		{
			content+=" ";
			//名字
			drawY += yOffset;
			g.setColor(TEXT_COLOR);
			g.drawString("压力：", xOffset, drawY);
			xOffset+= getLength("压力：")*contentfontSize;
			
			//变量值
			g.setColor(CONTENT_COLOR);
			g.drawString(content, xOffset, drawY);
			System.out.println(getLength(content));
			xOffset+= getLength(content)*contentfontSize;
			
			//单位
			g.setColor(UNIT_COLOR);
			g.drawString("MPa", xOffset, drawY);
			
		}
		
		xOffset = xClientOffset;
		content = dto.getLiuliang();
		if(content!=null)
		{
			content+=" ";
			//名字
			drawY += yOffset;
			g.setColor(TEXT_COLOR);
			g.drawString("流量：", xOffset, drawY);
			xOffset+= getLength("流量：")*contentfontSize;
			
			//变量值
			g.setColor(CONTENT_COLOR);
			g.drawString(content, xOffset, drawY);
			System.out.println(getLength(content));
			xOffset+= getLength(content)*contentfontSize;
			
			//单位
			g.setColor(UNIT_COLOR);
			g.drawString("M3/H", xOffset, drawY);
			
		}

		if(dto.getLiuliang()==null&&
				dto.getYali()==null&&
				dto.getYulv()==null&&
				dto.getZhuodu()==null)
		{
			drawY += yOffset;
			g.setColor(TEXT_COLOR);
			g.drawString("此监测点无数据！", xOffset, drawY);
		}
		
		
		g.dispose();
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		ImageOutputStream imOut;
		try {
			imOut = ImageIO.createImageOutputStream(bs);
			ImageIO.write((BufferedImage) image, "png", imOut);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		InputStream is = new ByteArrayInputStream(bs.toByteArray());
		return is;
	}

	/**
	 * 计算text的长度（一个中文算两个字符）
	 * 
	 * @param text
	 * @return
	 */
	public final static float getLength(String text) {
		int length = 0;
		for (int i = 0; i < text.length(); i++) {
			if (new String(text.charAt(i) + "").getBytes().length > 1) {
				length += 2;
			} else {
				length += 1;
			}
		}
		return length / 2.0f;
	}

	public static  InputStream drawWaterplant(GisTextureWaterplantDto dto, String path) {
		// TODO Auto-generated method stub
		File img = new File(path);
		Image src=null;
		try {
			src = ImageIO.read(img);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int width = src.getWidth(null);
		int height = src.getHeight(null);
		int xClientOffset = 60;
		int yClientOffset = 60;
		int yOffset =(height-2*yClientOffset)/4;
		int drawY = yClientOffset +yOffset/2;
//		int xOffset = xClientOffset+(width-2*xClientOffset) / 5;
		int xOffset = xClientOffset;
		int fontSize = yOffset / 5*2;
		int contentfontSize = yOffset / 3;
		Color titleColor = IMAGE_TITLE_COLOR;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.drawImage(src, 0, 0, width, height, null);
		g.setComposite(AlphaComposite
				.getInstance(AlphaComposite.SRC_OVER, 1.0f));
		//draw 监测点名字
		String name = dto.getName()+""+times++;
		System.out.println("刷新---->"+name);
		Font titleFont = new Font("黑体", Font.PLAIN, fontSize);
		g.setFont(titleFont);
		g.setColor(titleColor);
		g.drawString(name, (width - (getLength(name) * fontSize)) / 2, drawY);
		
		//draw 监测点属性
		Font contentFont = new Font("宋体", Font.PLAIN, contentfontSize);
		g.setFont(contentFont);
		String content = null;
		
		xOffset = xClientOffset;
		content = dto.getZhuodu();
		if(content!=null)
		{
			content+=" ";
			//名字
			g.setColor(TEXT_COLOR);
			drawY += yOffset;
			g.drawString("出厂浊度：", xOffset, drawY);
			xOffset+= getLength("出厂浊度：")*contentfontSize;
			
			//变量值
			g.setColor(CONTENT_COLOR);
			g.drawString(content, xOffset, drawY);
			System.out.println(getLength(content));
			xOffset+= getLength(content)*contentfontSize;
			
			//单位
			g.setColor(UNIT_COLOR);
			g.drawString("NTU", xOffset, drawY);
			
		}
		
		xOffset = xClientOffset;
		content = dto.getYulv();
		if(content!=null)
		{
			content+=" ";
			//名字
			g.setColor(TEXT_COLOR);
			drawY += yOffset;
			g.drawString("出厂余氯：", xOffset, drawY);
			xOffset+= getLength("出厂余氯：")*contentfontSize;
			
			//变量值
			g.setColor(CONTENT_COLOR);
			g.drawString(content, xOffset, drawY);
			System.out.println(getLength(content));
			xOffset+= getLength(content)*contentfontSize;
			
			//单位
			g.setColor(UNIT_COLOR);
			g.drawString("mg/L", xOffset, drawY);
			
		}
		
		
		xOffset = xClientOffset;
		content=dto.getGongshuiliang();
		if(content!=null)
		{
			content+=" ";
			//名字
			drawY += yOffset;
			g.setColor(TEXT_COLOR);
			g.drawString("供水量：", xOffset, drawY);
			xOffset+= getLength("供水量：")*contentfontSize;
			
			//变量值
			g.setColor(CONTENT_COLOR);
			g.drawString(content, xOffset, drawY);
			System.out.println(getLength(content));
			xOffset+= getLength(content)*contentfontSize;
			
			//单位
			g.setColor(UNIT_COLOR);
			g.drawString("MPa", xOffset, drawY);
			
		}
		

		if(dto.getGongshuiliang()==null&&
				dto.getYulv()==null&&
				dto.getZhuodu()==null)
		{
			drawY += yOffset;
			g.setColor(TEXT_COLOR);
			g.drawString("此水厂无监测数据！", xOffset, drawY);
		}
		
		
		g.dispose();
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		ImageOutputStream imOut;
		try {
			imOut = ImageIO.createImageOutputStream(bs);
			ImageIO.write((BufferedImage) image, "png", imOut);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		InputStream is = new ByteArrayInputStream(bs.toByteArray());
		return is;
	}
}
